package com.example.jinongheadlines.activities;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.jinongheadlines.R;
import com.example.jinongheadlines.models.Product;

public class ProductDetailActivity extends AppCompatActivity {

    private ImageView ivProductImage;
    private TextView tvProductName, tvProductDescription, tvProductPrice;
    private Button btnAddToCart, btnBuyNow;
    private ImageView ivCartIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        // 初始化视图
        initViews();

        // 获取传递的商品数据
        Product product = (Product) getIntent().getSerializableExtra("product");
        if (product != null) {
            displayProductDetails(product);
        }
    }

    private void initViews() {
        ivProductImage = findViewById(R.id.ivProductImage);
        tvProductName = findViewById(R.id.tvProductName);
        tvProductDescription = findViewById(R.id.tvProductDescription);
        tvProductPrice = findViewById(R.id.tvProductPrice);
        btnAddToCart = findViewById(R.id.btnAddToCart);
        btnBuyNow = findViewById(R.id.btnBuyNow);
        ivCartIcon = findViewById(R.id.ivCartIcon);

        // 设置点击事件
        btnAddToCart.setOnClickListener(v -> addToCart());
        btnBuyNow.setOnClickListener(v -> buyNow());
        ivCartIcon.setOnClickListener(v -> navigateToCart());
    }

    private void displayProductDetails(Product product) {
        Glide.with(this)
                .load(product.getImageUrl())
                .placeholder(R.drawable.placeholder_image)
                .into(ivProductImage);

        tvProductName.setText(product.getName());
        tvProductDescription.setText(product.getDescription());
        tvProductPrice.setText(String.format("¥%.2f", product.getPrice()));
    }

    private void addToCart() {
        // 添加到购物车逻辑
    }

    private void buyNow() {
        // 立即购买逻辑
    }

    private void navigateToCart() {
        // 跳转到购物车页面
    }
}